package cn.com.jinhao.provider.business.service.impl;


import cn.com.jinhao.hotline.commons.base.constant.SystemConstant;
import cn.com.jinhao.provider.business.entity.FlowInfo;
import cn.com.jinhao.provider.business.mapper.CommonMapper;
import cn.com.jinhao.provider.business.mapper.SysDictMapper;
import cn.com.jinhao.provider.business.service.FlowInfoService;
import cn.com.jinhao.hotline.commons.vo.user.OrganizerResponseVO;
import cn.com.jinhao.provider.business.vo.MemoInfo;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

/**
 * HotLine
 * <p>Description: </p>
 *
 * @author ShaneMew
 * @version 1.0
 * @date 2019/10/16
 * @since 2019-10-16
 */

public class CommonService {
    SysDictMapper sysDictMapper;
    FlowInfoService flowInfoService;
    CommonMapper commonMapper;
    protected StringBuffer code = new StringBuffer();


    /**
     * 根据编码类型获取编码
     *
     * @param codeType {@link SystemConstant.CodeType}
     * @return String   编码
     */
    String getCode(SystemConstant.CodeType codeType) {
        Date date = DateUtil.date();
        code.setLength(0);
        code.append(DateUtil.year(date));
        code.append(DateUtil.month(date));
        code.append(DateUtil.dayOfMonth(date));
        if (SystemConstant.CodeType.COMPLEX.getValue() == codeType.getValue()) {
            code.append(DateUtil.hour(date, true));
            code.append(DateUtil.minute(date));
            code.append(DateUtil.millsecond(date));
        }
        code.append(this.getRandom());
        return code.toString();
    }

    /**
     * 更新流程信息
     *
     * @param deptCode     部门编码
     * @param eventCode    事件编码
     * @param dispatchCode 派单编码
     * @param statusCode   状态编码
     * @param msg          信息
     */
    void updateFlowInfo(String deptCode, String eventCode, String dispatchCode, String statusCode, String msg) throws Exception {
        FlowInfo flowInfo = flowInfoService.findByEventCodeDeptCodeDispatchCode(deptCode, eventCode, dispatchCode);
        if (ObjectUtil.isNull(flowInfo)) {
            throw new Exception("查询不到当前流程状态信息...");
        }
        flowInfo.setStatusCode(statusCode);
        int result = flowInfoService.update(flowInfo);
        if (result == 0) {
            throw new Exception(msg + "事件流程插入失败!");
        }
    }

    /**
     * 新增流程信息
     *
     * @param eventCode 事件编码
     * @param deptCode  部门编码
     */
    void addFlowInfo(String eventCode, String deptCode) throws Exception {
        FlowInfo flowInfo = new FlowInfo();
        flowInfo.setEventCode(eventCode);
        flowInfo.setDeptCode(deptCode);
        flowInfo.setStatusCode(SystemConstant.FlowStatus.DISPATCH.getStatus());
        int result = flowInfoService.add(flowInfo);
        if (result == 0) {
            throw new Exception("增加派单流程出错...事件流程插入失败!");
        }
    }

    /**
     * 根据事件编码跟新流程信息
     *
     * @param eventCode    事件编码
     * @param deptCode     部门编码
     * @param dispatchCode 派单编码
     */
    void updateByEventCode(String eventCode, String deptCode, String dispatchCode) throws Exception {
        FlowInfo flowInfo = flowInfoService.queryByEventCode(eventCode);
        if (flowInfo == null) {
            throw new Exception("查询不到流程状态信息");
        }
        flowInfo.setDeptCode(deptCode);
        flowInfo.setDispatchCode(dispatchCode);
        flowInfo.setStatusCode(SystemConstant.FlowStatus.ACCEPT.getStatus());
        int result = flowInfoService.update(flowInfo);
        if (result == 0) {
            throw new Exception("派单失败....事件流程插入失败!");
        }
    }

    /**
     * 随机生成编码前缀
     *
     * @return String
     */
    private String getRandom() {
        int number = new Random().nextInt(99999);
        String codeNum = Integer.toString(number);
        switch (codeNum.length()) {
            case 1:
                codeNum += "0000";
                break;
            case 2:
                codeNum += "000";
                break;
            case 3:
                codeNum += "00";
                break;
            case 4:
                codeNum += "0";
                break;
            default:
                break;
        }
        return codeNum;
    }

    /**
     * 工单信息
     *
     * @param fromDept  发送部门
     * @param toDept    接受部门
     * @param operation 操作
     * @param code      工单
     * @return String
     */
    String getDetailMsg(String fromDept, String toDept, String operation, String code) {
        LocalDate todayDate = LocalDate.now();
        return "【" +
            todayDate.toString() +
            "】 - 【" +
            fromDept +
            "】向【" +
            toDept +
            "】" +
            operation +
            "了工单" +
            "【" +
            code +
            "】。";
    }

    String getDetailMsg(String oper,String workCode,int auditResult, String sourceOper) {
        LocalDate todayDate = LocalDate.now();
        if (Optional.ofNullable(sourceOper).isPresent()) {
            String oprationResult = "驳回了";
            if (auditResult == 1) {
                oprationResult = "通过了";
            }
            return "【" +
                    todayDate.toString() +
                    "】 - 【" +
                    oper +
                    oprationResult +
                    "【" +
                    sourceOper +
                    "】的撤单申请";
        }else {
            return "【" +
                    todayDate.toString() +
                    "】 - 【" +
                    oper +
                    "发起了工单【" +
                    workCode +
                    "】的撤单申请";
        }
    }

    /**
     * 备注信息
     *
     * @param memoInfo {@link MemoInfo}
     * @return String
     */
    String getMemo(MemoInfo memoInfo) {
        LocalDateTime todayTime = LocalDateTime.now();
        return "事件【" + memoInfo.getEventCode() + "】已于【" + todayTime + "】由【" + memoInfo.getFromDept() + "】的【" + memoInfo.getFromOperator() + "】通过【"
            + memoInfo.getWay() + "】交给【" + memoInfo.getToDept() + "】办理,交办联系人【" + memoInfo.getToOperator() + "】，派单编号【" + memoInfo.getCode() + "】";
    }

    @Transactional(rollbackFor = Exception.class)
    protected OrganizerResponseVO updateOrganizer(String eventCode, String deptCode) {
            OrganizerResponseVO organizerResponseVO = commonMapper.queryOriginalDept(eventCode, "BT$EVAS");
            String nowDept = commonMapper.queryNowDept(eventCode, deptCode, "BT$EVAS");
            organizerResponseVO.setNowDeptCode(deptCode);
            organizerResponseVO.setNowDept(nowDept);


            commonMapper.updateOriginalDept(eventCode, "BT$EVAS");
            commonMapper.updateNowDept(eventCode, deptCode, "BT$EVAS");

            return organizerResponseVO;

    }

    Date getExpiredDate(Date date) throws ParseException {
        // TODO: 调用系统字典里的信息
//        TbSysDict tbSysDict = new TbSysDict();
//        tbSysDict.setPaid(SystemConstant.SysDict.SYS_SETTING.getCode());
//        tbSysDict.setCode("6");
//        TbSysDict newTbSysDict = sysDictMapper.selectOne(tbSysDict);
        // 暂时使用模拟数据
        int time = 7;
        return DateUtil.offsetHour(date, time);
    }
}
